package com.lannuokeji.biz.items.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.lannuokeji.base.AController;
import com.lannuokeji.base.facade.PageRequest;
import com.lannuokeji.biz.items.entity.DeviceApply;
import com.lannuokeji.biz.items.entity.LabApply;
import com.lannuokeji.biz.items.service.LabApplyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 实验室申请 前端控制器
 * </p>
 *
 * @author lannuoTeck
 * @since 2018-03-19
 */
@Api(description="实验室借用申请")
@RestController
@RequestMapping("/items/labApply")
public class LabApplyController extends AController {

	@Resource
	LabApplyService labApplyService;
	
	@ApiOperation(value = "查询分页列表")
	@GetMapping()
	public Object getList(PageRequest pageRequest) {
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		page = labApplyService.selectPage(page);
		return setSuccessContent(page);
	}
	
	@ApiOperation(value = "分页查询申请列表，包括关联设备")
	@GetMapping("/withRecord")
	public Object pageDataWithChildren(PageRequest pageRequest, LabApply apply) {
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		page = labApplyService.pageWithChildren(page, apply);
		return setSuccessContent(page);
	}
	
	@ApiOperation(value = "添加实验室申请")
	@PostMapping()
	public Object create(HttpServletRequest request, @RequestBody LabApply entity) {
		entity.setUserId(getUser());
		boolean ret = labApplyService.insertAllColumn(entity);
		if (ret) {
			return setSuccessContent("添加成功！");
		} else {
			return setErrorContent("添加失败！");
		}
	}

	@ApiOperation(value = "根据id查询实验室申请")
	@GetMapping("/{id}")
	public Object get(HttpServletRequest request,
			@ApiParam(value = "实验室申请id", required = true) @PathVariable Integer id) {
		LabApply items = labApplyService.selectById(id);
		return setSuccessContent(items);
	}

	@ApiOperation(value = "删除实验室申请信息")
	@DeleteMapping("/{id}")
	public Object del(HttpServletRequest request,
			@ApiParam(value = "实验室申请id", required = true) @PathVariable Integer id) {
		if (labApplyService.deleteById(id)) {
			return setSuccessContent("删除成功");
		} else {
			return setErrorContent("删除失败");
		}
	}

	@ApiOperation(value = "修改实验室申请信息")
	@PutMapping()
	public Object update(HttpServletRequest request, @RequestBody LabApply entity) {
		if (labApplyService.updateById(entity)) {
			return setSuccessContent("修改成功！");
		} else {
			return setErrorContent("修改失败！");
		}
	}
}

